home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / ISR.SWG / 0008_WATCHDOG.PAS.pas < prev   
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  107 lines

  1.  BC> I have the desire to create a Type of "watch dog" Procedure For a Program
  2.  BC> I am writing. It has become increasingly attractive For me to create this
  3.  BC> Procedure to activate at certain times every hour - i.e. at 15 past and 45
  4.  BC> past the hour.
  5.  
  6. Perhaps if you insert this code into your Program. It works With the ISR 1C
  7. called by the timerinterrupt 8 periodically. For testing reasons it calls the
  8. batchFile at 15 and 45 seconds. Change finally the line "if(s=15) or (s=45)" to
  9. "if (m=15) or (m=45)" to test the minutes.
  10.  
  11. Code written in TP6.0 :
  12.  
  13. {$M 16384,0,16384} (*Adjust to your requirements*)
  14. Uses Dos,Crt;
  15.  
  16. Const
  17.   WDTryTime = 18; (*18 ticks = about every second*)
  18.   BatchFileName = 'woufwouf.bat'; (*BatchFile to start*) <<<your BatchFilename
  19. Var
  20.   WDCount : Integer;
  21.   WDBusy,WdEvent : Boolean;
  22.   WDSave1c,WDSaveExit: Pointer;
  23.   DosReentrant : ^Byte;
  24.   IntVec : Array[0..255] of Pointer Absolute 0:0;
  25.  
  26. Procedure STI;
  27. Inline($FB);
  28.  
  29. Procedure CLI;
  30. Inline($FA);
  31.  
  32. (*Do not use Turbo-Pascal I/O Routines in here.*)
  33. (*Be also sure not to use GetTime/Date at the same time as this routine*)
  34.  
  35. Procedure WatchDog;interrupt;
  36. Var h,m,s,c : Word;
  37. begin
  38.   if not WDBusy then
  39.   begin
  40.     WDBusy:=True;
  41.     inc(WDCount);
  42.     if DosReentrant^=0 then      (*No Dos op's in work ?*)
  43.       if WDCount>=WdTryTime then (*Test only every second to prevent*)
  44.       begin                      (*big loss in perFormance.        *)
  45.         WDCount:=0;
  46.         GetTime(h,m,s,c);              (*Get Time*)
  47.  
  48. here>>> if(s=15) or (s=45) then (*Call on 15 minutes and 45 minutes*)
  49.  
  50.         begin
  51.           Cli;IntVec[$1c]:=WDSave1C;Sti;
  52.           Port[$20]:=$a0;(*Report TimerInt finished to Interrupt-contr.*)
  53.           Port[$20]:=$20;
  54.           SwapVectors;      (*Execute COMMand.COM + batchFile*)
  55.           Exec(GetEnv('COMSPEC'),'/C '+batchFilename);
  56.           SwapVectors;
  57.           Cli;IntVec[$1c]:=@Watchdog;Sti;
  58.           WDEvent:=True;
  59.         end;
  60.       end;
  61.     WDBusy:=False;
  62.   end;
  63. end;
  64.  
  65. Procedure WDRemove;
  66. begin
  67.   SetIntVec($1C,WDSave1c); (*Restore old 1c routine*)
  68.   ExitProc:=WDSaveExit;
  69.   Writeln('Exiting Watchdog');
  70.   Halt(Exitcode);
  71. end;
  72.  
  73. Procedure WDInstall;
  74. Var Regs:Registers;
  75. begin
  76.   With Regs do
  77.   begin
  78.     ah:=$34;
  79.     MsDos(regs);
  80.     DosReentrant:=ptr(es,bx); (*Get Reentrant Pointer*);
  81.   end;
  82.   WdCount:=0;
  83.   WDBusy:=False;
  84.   WDEvent:=False;
  85.   GetIntVec($1C,WDSave1C); (*Save old 1c routine*)
  86.   SetIntVec($1C,@Watchdog); (*Assign Watchdog to int 1c*)
  87.   WDSaveExit:=ExitProc;
  88.   ExitProc:=@WDRemove;
  89.   Writeln('Watchdog installed');
  90. end;
  91.  
  92. Var c:Char;
  93.  
  94. begin
  95.   WDInstall;
  96.   Repeat (*Example Main Loop*)
  97.     Write('Hello');
  98.     if WDEvent then  (*Watch sign of Watchdog*)
  99.     begin
  100.       Writeln;Writeln('Event occured');
  101.       WDEvent:=False;
  102.     end;
  103.     Delay(1000);
  104.   Until KeyPressed;
  105.   c:=ReadKey;
  106. end.
  107.